package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Question;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IQuestionService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("questions")
public class QuestionController {
    @Autowired
    private IQuestionService questionService;
    @GetMapping
    public PageInfo list(QueryObject qo){
        //设置按创建时间倒序排序
        qo.setOrderBy("q.createTime desc");
        return questionService.query(qo);
    }

    @PostMapping
    public JSONResult save(Question question){
        User currentUser = UserContext.getCurrentUser();
        question.setUser(currentUser);
        questionService.save(question);
        return new JSONResult();
    }
}
